package com.shinektv.utils;

import java.util.Arrays;

public class ByteUtils {
	// 使用 Arrays.copyOf() 方法,但要在 java6++版本中
	public static  byte[] concat(byte[] first, byte[] second) {
		byte[] result = Arrays.copyOf(first, first.length + second.length);
		System.arraycopy(second, 0, result, first.length, second.length);
		return result;
	}

	/**
	 * 合并byte[]数组 （不改变原数组）
	 * @param bt1
	 * @param bt2
	 * @return 合并后的数组
	 */
	public static byte[] byteMerger(byte[] bt1, byte[] bt2){
		byte[] bt3 = new byte[bt1.length+bt2.length];
		System.arraycopy(bt1, 0, bt3, 0, bt1.length);
		System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
		return bt3;
	}
	
	/**
	 * 截取byte数组   不改变原数组
	 * @param b 原数组
	 * @param off 偏差值（索引）
	 * @param length 长度
	 * @return 截取后的数组
	 */
	public static byte[] subByte(byte[] b,int off,int length){
		byte[] b1 = new byte[length];
		System.arraycopy(b, off, b1, 0, length);
		return b1;
	}
	/**
	 * 小端 int to byte[] 支持 1或者 4 个字节
	 * @param i
	 * @param len
	 * @return
	 */
	public static byte[] little_intToByte(int i, int len) {
		byte[] abyte = new byte[len];
		if (len == 1) {
			abyte[0] = (byte) (0xff & i);
		} else if (len == 2) {
			abyte[0] = (byte) (0xff & i);
			abyte[1] = (byte) ((0xff00 & i) >> 8);
		} else {
			abyte[0] = (byte) (0xff & i);
			abyte[1] = (byte) ((0xff00 & i) >> 8);
			abyte[2] = (byte) ((0xff0000 & i) >> 16);
			abyte[3] = (byte) ((0xff000000 & i) >> 24);
		}
		return abyte;
	}

	public static int little_bytesToInt(byte[] bytes) {
		int addr = 0;
		if (bytes.length == 1) {
			addr = bytes[0] & 0xFF;
		} else if (bytes.length == 2) {
			addr = bytes[0] & 0xFF;
			addr |= (((int) bytes[1] << 8) & 0xFF00);
		} else {
			addr = bytes[0] & 0xFF;
			addr |= (((int) bytes[1] << 8) & 0xFF00);
			addr |= (((int) bytes[2] << 16) & 0xFF0000);
			addr |= (((int) bytes[3] << 24) & 0xFF000000);
		}
		return addr;
	}

	/**
	 * 大端 int to byte[] 支持 1或者 4 个字节
	 * @param i
	 * @param len
	 * @return
	 */
	public static byte[] big_intToByte(int i, int len) {
		byte[] abyte = new byte[len];
		;
		if (len == 1) {
			abyte[0] = (byte) (0xff & i);
		} else if (len == 2) {
			abyte[0] = (byte) ((i >>> 8) & 0xff);
			abyte[1] = (byte) (i & 0xff);
		} else {
			abyte[0] = (byte) ((i >>> 24) & 0xff);
			abyte[1] = (byte) ((i >>> 16) & 0xff);
			abyte[2] = (byte) ((i >>> 8) & 0xff);
			abyte[3] = (byte) (i & 0xff);
		}
		return abyte;
	}

	public static int big_bytesToInt(byte[] bytes) {
		int addr = 0;
		if (bytes.length == 1) {
			addr = bytes[0] & 0xFF;
		} else if (bytes.length == 2) {
			addr = bytes[0] & 0xFF;
			addr = (addr << 8) | (bytes[1] & 0xff);
		} else {
			addr = bytes[0] & 0xFF;
			addr = (addr << 8) | (bytes[1] & 0xff);
			addr = (addr << 8) | (bytes[2] & 0xff);
			addr = (addr << 8) | (bytes[3] & 0xff);
		}
		return addr;
	}
	// 获得本机ＣＰＵ大小端
	public static boolean isBigendian() {
		short i = 0x1;
		boolean bRet = ((i >> 8) == 0x1);
		return bRet;
	}

	public static  byte[] intToBytes(int length)
	{
		byte[] bytes = new byte[4];
		bytes[0] = (byte)((length & 0xFF000000) >> 24);
		bytes[1] = (byte)((length & 0xFF0000) >> 16);
		bytes[2] = (byte)((length & 0xFF00) >> 8);
		bytes[3] = (byte)((length & 0xFF) );
		return bytes;
	}

	/**
	 * 获取消息体长度
	 * @param bytes
	 * @return
	 */
	public static int bytesToInt(byte bytes[])
	{
		if(bytes==null || bytes.length<4)
		{
			return 0;
		}
		int ret=  ((bytes[0]&0xff)<< 24) + ((bytes[1]&0xff) << 16) + ((bytes[2]&0xff) << 8) +(bytes[3]&0xff);
		return ret;
	}


	private static short getShort(byte[] src, int start)
	{
		return (short)((src[start] & 0xFF) | (src[start+1] << 8));
	}

	/**
	 * 调节PCM数据音量16K声道
	 * src  :PCM 数据
	 * multiple: 放大倍数，如1.5
	 */
	public static void amplifyPCMData(byte[] src, float multiple)
	{
		amplifyPCMData(src,src.length,multiple);
	}
	/**
	 * 调节PCM数据音量16K声道
	 * src  :PCM 数据
	 * multiple: 放大倍数，如1.5
	 */
	public static void amplifyPCMData(byte[] src,int length,float multiple)
	{
		int nCur = 0;
		while (nCur < length)
		{
			short volum = getShort(src, nCur);
			//Log.d(TAG, "volum="+volum);
			volum = (short)(volum * multiple);
			if (volum < (short)-0x7F00)
			{
				volum = (short)-0x7F00;
			}
			else if (volum > (short) 0x7F00)//爆音的处理
			{
				volum = (short) 0x7F00;
			}

			src[nCur]   = (byte)( volum       & 0xFF);
			src[nCur+1] = (byte)((volum >> 8) & 0xFF);
			nCur += 2;
		}
	}
}
